<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\GradeRepository;
use HiCommon\Repository\EquitiesRepository;
use HiCommon\Repository\EquitiesExtendsRepository;

class GradeService{

    /**
     *  等级列表
     *  hinq 2019 08 21
     *
     **/
    public function get_list( array $arr_search ){
        $arr_where['order'] =['key'=>'min_integral','value'=>'asc'];
        $arr_list = GradeRepository::get_list( (array)$arr_search , (array)$arr_where );
        $arr_list->appends($arr_search);//添加链接参数
        $arr_list = list_page_data( $arr_list , $arr_search );
        return hello_success('成功',$arr_list);
    }

    /**
     *  权益列表
     *  hinq 2019 08 21
     *
     **/
    public function get_list_equities( array $arr_search ){
        $arr_option['order'] =['key'=>'sort','value'=>'asc'];
        $arr_list = EquitiesRepository::get_list( (array) $arr_search , (array) $arr_option );
        $arr_list->appends($arr_search);//添加链接参数
        $arr_list = list_page_data( $arr_list , $arr_search );
        foreach ($arr_list['data'] as $key => &$value) {
            $value['full_src'] = oss_full_url($value['img']);
        }
        return hello_success('成功',$arr_list);
    }


    /**
     *  获取指定的一条数据
     *  hinq 2019 08 21
     *
     **/
    public function get_one( int $nId ){
        $arr_where['field'] = ['id','name','min_integral','equities','max_integral'];
        $arr_info = GradeRepository::get_one( (int) $nId , (array) $arr_where );
        if( !empty(  $arr_info['equities'] ) ){
            $arr_info['equities'] = explode( ',', $arr_info['equities'] );
        }
        return $arr_info;
    }


    /**
     *  获取指定的一条数据
     *  hinq 2019 08 23
     *
     **/
    public function getOneEquities( int $nId ){
        $arr_where['field'] = ['id','name','remark','img','sort'];
        $arr_list = EquitiesRepository::get_one( (int) $nId , (array) $arr_where );
        $arr_list['img'] =  oss_full_url( $arr_list['img'] );
        return $arr_list;
    } 

    /**
     *  添加会员等级
     *  hinq 2019 08 22
     *
     **/
    public function add_one( array $arrInput ){

        try {

            if( empty( $arrInput['name'] ) ){
                return hello_error('VIP会员等级名称不能为空');
            }
            if( empty( $arrInput['min_integral'] ) ){
                return hello_error('请设置最低积分');
            }

            if( empty( $arrInput['max_integral'] ) ){
                return hello_error('请设置最高积分');
            }
            $arrInput['min_integral'] = intval( floor( $arrInput['min_integral'] ));   //先向下取整 在取整
            $arrInput['max_integral'] = intval( floor( $arrInput['max_integral'] ));   //先向下取整 在取整

            if( $arrInput['min_integral'] == $arrInput['max_integral']){
                return hello_error('最低积分不能等于最高积分');
            }

            if( $arrInput['min_integral'] > $arrInput['max_integral'] ){
                $temp = $arrInput['max_integral'];
                $arrInput['max_integral'] = $arrInput['min_integral'];
                $arrInput['min_integral'] = $temp;
            }

            //当前添加的等级名称是否存在
            $map['name'] = $arrInput['name'];
            $map_info = GradeRepository::get_where_count( (array) $map );
            if( $map_info > 0 ){
                return hello_error('VIP会员等级名称已存在');
            }

            //当前最大积分是否存在等级内

            // //最小积分是否在别的等级范围内
            // $map['min_integral'] = [ '<=' , $arrInput['min_integral'] ];
            // $map['max_integral'] = [ '>=' , $arrInput['min_integral'] ];
            // $map_info = GradeRepository::get_where_one( (int) $arrInput['min_integral'] ,   (int) $arrInput['max_integral'] )->toArray();
            // if( !empty( $map_info ) ){
            //     return hello_error('最低积分已在 【'.$map_info['name']. '】范围内');
            // }
            // //最高积分是否在别的等级范围内
            // $map['min_integral'] = [ '<=' , $arrInput['max_integral'] ];
            // $map['max_integral'] = [ '>=' , $arrInput['max_integral'] ];
            // $map_info = GradeRepository::get_where_one( (array) $map )->toArray();
            // if( !empty( $map_info ) ){
            //     return hello_error('最高积分已在 【'.$map_info['name']. '】范围内');
            // }

            //添加
            $res = GradeRepository::add_one( (array) $arrInput );
            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }

    }


    /**
     *  添加权益
     *  hinq 2019 08 23
     *
     **/
    public function add_one_equities( array $arrInput ){

        try {
            //添加
            $res = EquitiesRepository::add_one( (array) $arrInput );
            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }



    /**
     *  编辑会员等级
     *  hinq 2019 08 22
     *
     **/
    public function edit_one( array $arrInput ){
        try {
            //编辑
            unset( $arrInput['_token']);
        
            if( empty( $arrInput['id'] ) ){
                if( empty( $arrInput['name'] ) ){
                    return hello_error('VIP会员等级名称不能为空');
                }
                if( empty( $arrInput['min_integral'] ) ){
                    return hello_error('请设置最低积分');
                }
                if( empty( $arrInput['max_integral'] ) ){
                    return hello_error('请设置最高积分');
                }
                $arrInput['min_integral'] = intval( floor( $arrInput['min_integral'] ) );
                $arrInput['max_integral'] = intval( floor( $arrInput['max_integral'] ) );
            }
            //当前添加的等级名称是否存在
            // $map['name'] = $arrInput['name'];
            // $map_info = GradeRepository::get_where_count( (array) $map );
            // if( $map_info > 0 ){
            //     return hello_error('VIP会员等级名称已存在');
            // }


            if( !empty( $arrInput['equities'] ) ){
                $arrInput['equities'] = trim(  $arrInput['equities'] ,',' );
            }
            $res = GradeRepository::edit_one( (array) $arrInput );
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  编辑权益数据
     *  hinq 2019 08 23
     *
     **/
    public function edit_one_equities( array $arrInput ){

        try {
            //编辑
            unset( $arrInput['_token']);
            $res = EquitiesRepository::edit_one( (array) $arrInput );
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            
            return hello_success('编辑成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }


    }


    /**
     *  删除指定数据
     *  hinq 2019 08 22
     *
     **/
    public function delete_one( int $nId ){
        try {
            //删除
            $res = GradeRepository::delete_one( (int) $nId );
            $result = hi_check_res($res,'delete_one');
            if ($result['state'] != 0) {
                return $result;
            }
            return $result;
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  删除指定的权益数据
     *  hinq 2019 08 23
     *
     **/
    public function delete_one_equities( int $nId ){
        try {
            //删除
            $res = EquitiesRepository::delete_one( (int) $nId );
            if (empty($res)) {
                return hello_error('删除失败');
            }

            return hello_success('成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }



    /**
     *  添加权益礼包数据 
     *  hinq 2019 08 23
     *
     **/
    public function add_one_gifts( array $arrInput ){
        // \DB::beginTransaction();
        try {
            //添加
            //获取当前权益的信息
            $arr_where['field'] = ['id','is_gifts'];
            $arr_info = EquitiesRepository::get_one( (int) $arrInput['id'] ,(array) $arr_where );
           
            //获取当前权益是否已添加了当前优惠券
            $arr_where2 = ['link_id'=> $arrInput['id'] , "coupon_id"=>$arrInput['coupon_id']]; 
            $count = EquitiesExtendsRepository::get_where_count( $arr_where2 );
            if( $count > 0 ){
                return hello_error('该优惠券已设置');
            }

            //修改权益的熟悉
            if( $arr_info['is_gifts'] == 0 ){
                $temp['id'] = $arrInput['id'];
                $temp['is_gifts'] = 1;
                EquitiesRepository::edit_one( (array) $temp );
            }
            $arrInput['link_id'] = $arrInput['id'];
            unset( $arrInput['id'] );
            EquitiesExtendsRepository::add_one( (array) $arrInput );
            \DB::commit();
            return hello_success('设置成功', '', []);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }

    }


    /**
     *  编辑权益礼包数据
     *  hinq 2019 08 27
     *
     **/
    public function edit_one_gifts( array $arrInput ){
        try {
            //编辑
            unset( $arrInput['_token']);
            $res = EquitiesExtendsRepository::edit_one( (array) $arrInput );
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }

    }



    /**
     *  根据条件获取指定的礼包数据
     *  hinq 2019 08 23
     *
     **/
    public function get_where_list_fo_gifts( array $arrWhere ){
        return EquitiesExtendsRepository::get_all( (array) $arrWhere );
    }

    /**
     *  删除指定的礼包数据
     *  hinq 2019 08 23
     *
     **/
    public function del_one( int $nId ){
        try {
            //删除
            $res = EquitiesExtendsRepository::delete_one( (int) $nId );
            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  获取所有的权益数据
     *  hinq 2019 08 23
     *
     **/
    public function get_all(){
        $arrWhere['field'] = ['id','name'];
        $arrWhere['order'] = ['key'=>'id','value'=>'asc'];
        return EquitiesRepository::get_all( (array) $arrWhere );
    }


}